package com.sunmi.usercenter.ui.view;

import java.io.File;

import com.sunmi.usercenter.bean.Info;
import com.sunmi.usercenter.bean.Task;
import com.sunmi.usercenter.utils.ThreadPoolManager;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * 自定义ViewGroup超类
 * 
 * @author longtao.li
 * 
 */
@SuppressWarnings("rawtypes")
public abstract class SuperLayout extends FrameLayout implements SuperUI, OnClickListener {

	public SuperLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		_init();
	}

	public SuperLayout(Context context) {
		super(context);
		_init();
	}

	public SuperLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		_init();
	}

	protected final String INFO_NAME = "info";
	
	protected Context mContext;
	protected Activity mActivity;
	protected ProgressDialog progress;
	protected ThreadPoolManager mThreadPoolManager;

	protected void _init() {
		mContext = getContext();
		mActivity = (Activity) mContext;
		mThreadPoolManager = ThreadPoolManager.getInstance();
	}

	/**
	 * 当前最后执行的线程任务,task的ID属性可以用于判断线程启动的先后
	 */
	protected Task lastTask = new Task(0) {

		@Override
		public void run() {
			// TODO Auto-generated method stub
		}
	};

	/**
	 * 弹出土司
	 * 
	 * @param msg
	 */
	public void showToastOnUI(final String msg) {
		mActivity.runOnUiThread(new Runnable() {

			@Override
			public void run() {
				Toast.makeText(mActivity, msg, Toast.LENGTH_SHORT).show();
			}
		});

	}

	protected void hintNoNetWork() {
		// TODO 弹出网络错误提示框
	}

	protected float getDimen(int id) {
		float dimension = getResources().getDimension(id);
		return dimension;
	}

	public void executeTask(Runnable run) {
		mThreadPoolManager.executeTask(run);
	}

	/**
	 * 加载图片
	 * 
	 * @param tuContainer
	 * @param item
	 */
	public void loadIMG(ImageView img, String url) {
//		Picasso.with(mContext).load(url).error(R.drawable.error).into(img);
	}

	public void loadIMG(ImageView img, int id) {
//		Picasso.with(mContext).load(id).error(R.drawable.error).into(img);
	}

	/**
	 * 打开一个Activity
	 * 
	 * @param clazz
	 * @param info
	 */
	public void openActivity(final Class clazz, final Info info) {
		runOnUI(new Runnable() {

			@Override
			public void run() {
				Intent intent = new Intent();
				intent.setClass(mContext, clazz);
				if (info != null) {
					intent.putExtra(INFO_NAME, info);
				}
				mContext.startActivity(intent);
			}
		});

	}

	/**
	 * 打开一个Activity for result
	 * 
	 * @param clazz
	 * @param info
	 */
	public void openActivity4Result(final Class clazz, final Info info,
			final int requestCode) {
		runOnUI(new Runnable() {

			@Override
			public void run() {
				Intent intent = new Intent();
				intent.setClass(mContext, clazz);
				if (info != null) {
					intent.putExtra(INFO_NAME, info);
				}
				mActivity.startActivityForResult(intent, requestCode);
			}
		});

	}

	/**
	 * UI线程执行一个任务
	 * 
	 * @param run
	 */
	public void runOnUI(Runnable run) {
		mActivity.runOnUiThread(run);
	}

	/**
	 * 获得配置文件参数
	 * 
	 * @param id
	 * @return
	 */
	public float getDimens(int id) {
		return getResources().getDimension(id);
	}
	
	/**
	 * 获得配置文件参数
	 * 
	 * @param id
	 * @return
	 */
	public int getDimensPixelSize(int id) {
		return getResources().getDimensionPixelSize(id);
	}

	@Override
	public void executeTask(Task task) {
		this.lastTask = task;
		mThreadPoolManager.executeTask(task);

	}

	@Override
	public void showProgressDialog() {
		runOnUI(new Runnable() {

			@Override
			public void run() {
				if (progress == null) {
					progress = new ProgressDialog(mContext);
					progress.setMessage("正在加载,请稍后...");
					progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
				}
				progress.setCancelable(true);
				try {
					progress.show();
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		});
	}

	@Override
	public void dismissProgressDialog() {
		runOnUI(new Runnable() {

			@Override
			public void run() {
				if (progress != null) {
					progress.dismiss();
				}

			}
		});
	}

	@Override
	public void hideSoftInput(Context context, View achor) {
		InputMethodManager imm = (InputMethodManager) context
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(achor.getWindowToken(),
				InputMethodManager.HIDE_NOT_ALWAYS);

	}

	@Override
	public boolean hasInfo() {
		return false;
	}


	@Override
	public void closeActivity4Result(int resultCode, Info data) {
		Intent intent = mActivity.getIntent();
		intent.putExtra(INFO_NAME, data);
		mActivity.setResult(resultCode, intent);
		mActivity.finish();

	}

	@Override
	public void loadIMG(ImageView img, String url, int errorId) {
//		Picasso.with(mContext).load(url).error(R.drawable.error).placeholder(errorId).into(img);

	}

	@Override
	public void loadIMG(ImageView img, File file) {
//		Picasso.with(mContext).load(file).error(R.drawable.error).placeholder(R.drawable.error).into(img);

	}

	@Override
	public OnClickListener getClickListener() {
		return this;
	}

	@Override
	public void loadIMG(ImageView img, String url, int placeholder, int errorId) {
//		Picasso.with(mContext).load(url).error(errorId)
//				.placeholder(placeholder).into(img);

	}


}
